<?php
/**
 * @file Db.php
 *
 * Database representation.
 *
 * With this class you can interact with the databases. It uses by default the
 * connection in configuration.php for establishing a connection.
 *
 * New BSD License
 * \n
 * Copyright (c) 2010 Rpd-Team
 *
 * @namespace  rpd
 * @since      0.0.1
 * @version    0.0.1
 */

/**
 *
 */

class Db {
	protected $_sql;

	function database($name)
	{
		$this->_sql->database($name);
	}

	function insert_id()
	{
		return $this->_sql->insert_id();
	}

	function query($statement)
	{
		if (func_num_args() > 1)
		{
			$a = func_get_args();
			array_shift($a);
			return $this->_sql->query($statement, $a);
		}

		return $this->_sql->query($statement);
	}

	function __construct($settings = NULL)
	{
		if ($settings === NULL)
		{
			$settings = Registry::get("db/std");
		}

		$class = "Db_Adapter_" . strtolower($settings["driver"]);
		if (class_exists($class))
		{
			$this->_sql = new $class($settings);
		}
		else
		{
			throw new Error("Unknown driver " . $settings["driver"]);
		}

		$this->_sql->database($settings["database"]);
		$this->_sql->encoding($settings["encoding"]);
	}
}

?>